//
//  TravelSearchListController.swift
//  AirTravel
//
//  Created by apple on 2021/7/28.
//

import UIKit
import HandyJSON
class TravelSearchListController: UIViewController {
    /// 出行天数
    @IBOutlet weak var numberDaysBtn: UIButton!
    /// 出行人数
    @IBOutlet weak var numberBtn: UIButton!
    /// 智能排序
    @IBOutlet weak var smartBtn: UIButton!
    /// 价格
    @IBOutlet weak var priceBtn: UIButton!
    
    var ruleData:CityRuleMianModel?
    
    var SpotListData:[CitySpotListModel]?
    
    
    var currentPageIndex = 1
    
    //页面数据
    
    var sortType:String = "1"
    
    var minPrice:String = "0"
    
    var maxPrice:String = "2600"
    
    var keyword:String = ""
    
    var number:String = "1"
    
    var day:String = "1"
    var currCityIndex = ""
    
    
    @IBOutlet weak var tabView: UITableView!
    let identifySS = "ScenicSpotsCell"
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        currCityIndex = "28"
        
        smartBtn.setTitleColor(UIColor.init(hexString: "5DDEA7"), for: .normal)
        numberBtn.setTitleColor(UIColor.black, for: .normal)
        priceBtn.setTitleColor(UIColor.black, for: .normal)
        numberDaysBtn.setTitleColor(UIColor.black, for: .normal)
        
        smartBtn.setImage(UIImage(named: "多边形1拷贝-2"), for: .normal)
        numberBtn.setImage(UIImage(named: "多边形1拷贝"), for: .normal)
        priceBtn.setImage(UIImage(named: "多边形1拷贝"), for: .normal)
        numberDaysBtn.setImage(UIImage(named: "多边形1拷贝"), for: .normal)
        

        self.tabView.dataSource = self
        self.tabView.delegate = self
        self.tabView.rowHeight = 163
        self.tabView.tableFooterView = UIView.init()
        self.tabView.register(UINib.init(nibName: "ScenicSpotsCell", bundle: nil), forCellReuseIdentifier: identifySS)
       
        
        
        searchData()
    }

    @IBAction func list1Click(_ sender: Any) {
        smartBtn.setTitleColor(UIColor.init(hexString: "5DDEA7"), for: .normal)
        numberBtn.setTitleColor(UIColor.black, for: .normal)
        priceBtn.setTitleColor(UIColor.black, for: .normal)
        numberDaysBtn.setTitleColor(UIColor.black, for: .normal)
        
        smartBtn.setImage(UIImage(named: "多边形1拷贝-2"), for: .normal)
        numberBtn.setImage(UIImage(named: "多边形1拷贝"), for: .normal)
        priceBtn.setImage(UIImage(named: "多边形1拷贝"), for: .normal)
        numberDaysBtn.setImage(UIImage(named: "多边形1拷贝"), for: .normal)
        
        var arr:[String] = []
        
        for model in (ruleData?.data?.sortList)!{
            arr.append(model.name)
        }
        
        
        BRStringPickerView.showPicker(withTitle: "排序", dataSourceArr: arr, select: 0) { (model) in
            self.sortType = "\(self.ruleData?.data?.sortList?[model!.index].id ?? 1)"
            
        }
    }
    
    
    @IBAction func list2Click(_ sender: Any) {
        
        smartBtn.setTitleColor(UIColor.black, for: .normal)
        numberBtn.setTitleColor(UIColor.black, for: .normal)
        priceBtn.setTitleColor(UIColor.init(hexString: "5DDEA7"), for: .normal)
        numberDaysBtn.setTitleColor(UIColor.black, for: .normal)
        
        smartBtn.setImage(UIImage(named: "多边形1拷贝"), for: .normal)
        numberBtn.setImage(UIImage(named: "多边形1拷贝"), for: .normal)
        priceBtn.setImage(UIImage(named: "多边形1拷贝-2"), for: .normal)
        numberDaysBtn.setImage(UIImage(named: "多边形1拷贝"), for: .normal)
        
        var arr:[String] = []
        
        for model in (ruleData?.data?.priceList?.first?.infolist)!{
            arr.append(model.keyword)
        }
        
        
        BRStringPickerView.showPicker(withTitle: "价格", dataSourceArr: arr, select: 0) { (model) in

            
            self.minPrice = "\(self.ruleData?.data?.priceList?.first?.infolist?[model!.index].min ?? 0)"
            
            self.maxPrice = "\(self.ruleData?.data?.priceList?.first?.infolist?[model!.index].max ?? 2600)"
            
        }
    }
    
    
    @IBAction func list3Click(_ sender: Any) {
        
        smartBtn.setTitleColor(UIColor.black, for: .normal)
        numberBtn.setTitleColor(UIColor.init(hexString: "5DDEA7"), for: .normal)
        priceBtn.setTitleColor(UIColor.black, for: .normal)
        numberDaysBtn.setTitleColor(UIColor.black, for: .normal)
        
        smartBtn.setImage(UIImage(named: "多边形1拷贝"), for: .normal)
        numberBtn.setImage(UIImage(named: "多边形1拷贝-2"), for: .normal)
        priceBtn.setImage(UIImage(named: "多边形1拷贝"), for: .normal)
        numberDaysBtn.setImage(UIImage(named: "多边形1拷贝"), for: .normal)
        
        
        
        var arr:[String] = []
        
        for model in (ruleData?.data?.numberList?.first?.infolist)!{
            arr.append(model.name)
        }
        
        
        BRStringPickerView.showPicker(withTitle: "出行人数", dataSourceArr: arr, select: 0) { (model) in
            self.number = "\(self.ruleData?.data?.numberList?.first?.infolist?[model!.index].id ?? 1)"
            
        }
    }
    
    
    @IBAction func list4Click(_ sender: Any) {
        
        smartBtn.setTitleColor(UIColor.black, for: .normal)
        numberBtn.setTitleColor(UIColor.black, for: .normal)
        priceBtn.setTitleColor(UIColor.black, for: .normal)
        numberDaysBtn.setTitleColor(UIColor.init(hexString: "5DDEA7"), for: .normal)
        
        smartBtn.setImage(UIImage(named: "多边形1拷贝"), for: .normal)
        numberBtn.setImage(UIImage(named: "多边形1拷贝"), for: .normal)
        priceBtn.setImage(UIImage(named: "多边形1拷贝"), for: .normal)
        numberDaysBtn.setImage(UIImage(named: "多边形1拷贝-2"), for: .normal)
        var arr:[String] = []
        
        for model in (ruleData?.data?.dayList?.first?.infolist)!{
            arr.append(model.name)
        }
        
        
        BRStringPickerView.showPicker(withTitle: "出行天数", dataSourceArr: arr, select: 0) { (model) in
            print("\(model?.index)  \(model?.value)")
            
            self.day = "\(self.ruleData?.data?.dayList?.first?.infolist?[model!.index].id ?? 1)"
            
        }
    }
    
    
    
    
    
    func searchData(){
        
        
        let dic = ["sortType" : sortType , "cityId":  currCityIndex , "pageIndex":1 , "pageCount":100 , "minPrice" : minPrice,"maxPrice" : maxPrice,"keyword" : keyword,"number" : number,"day" : day] as [String : Any]
        
        NetWorkRequest(API1.getScenicSpotList(parameters: dic)) { responds in
            guard let model = JSONDeserializer<CitySpotListMianModel>.deserializeFrom(json: "\(responds)") else {
                return
                
            }
            
            self.SpotListData = model.data
            self.tabView.reloadData()
            
            
        }
        
    }
    
}

extension TravelSearchListController:UITableViewDataSource,UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return SpotListData?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: identifySS) as! ScenicSpotsCell
        cell.img.kf.setImage(with: URL(string: SpotListData?[indexPath.row].image_url ?? ""))
        cell.titleLab.text = SpotListData?[indexPath.row].name
        cell.contenLab.text = SpotListData?[indexPath.row].product_description
      
  
        cell.contenLab.text = SpotListData?[indexPath.row].product_description.filterHTML()!
        cell.oldPriceLab.text = "￥" + (SpotListData?[indexPath.row].ctrip_price)!
        cell.priceLab.text = "￥" + (SpotListData?[indexPath.row].price)!
        
        cell.soldLab.text =  SpotListData?[indexPath.row].province_name
        
        
        cell.selectionStyle = .none
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "TravelDetailViewController") as! TravelDetailViewController
        vc.dataModel = SpotListData?[indexPath.row]
        self.navigationController?.pushViewController(vc, animated: true)
        
        
//        let vc = WebLoadingViewController()
//        vc.tag = 10
//        vc.filePath = SpotListData?[indexPath.row].product_description ?? ""
//        let cvc = getCurrentVc()
//        cvc.navigationController?.pushViewController(vc,animated: true)
    }
    
}
